<template>
  <div>
    <!-- 卡片区 -->
    <el-card>
      <el-date-picker
        v-model="selectDate"
        type="date"
        clearable
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd"
        placeholder="选择日期"
        @change="changeDate"
      >
      </el-date-picker>
      <el-button style="margin-left: 20px" type="primary" @click="onSubmit"
        >发送代码统计邮件</el-button
      >
      <div style="padding: 0 0 20px; box-sizing: border-box">
        <div v-html="dailyHtml"></div>
      </div>
    </el-card>
  </div>
</template>

<script>
import { reSendmailSpecDate } from '@/api/sendmail';
import { previewEmail } from '@/api/codeCount';

export default {
  created() {},
  data() {
    return {
      selectDate: null,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      dailyHtml: null,
    };
  },
  methods: {
    async changeDate(nyrDate) {
      if (!nyrDate) {
        this.dailyHtml = null;
        return;
      }
      try {
        this.dailyHtml = await previewEmail(nyrDate);
      } catch (err) {
        console.log('changeDate --> ', err);
        this.dailyHtml = null;
      }
    },
    async onSubmit() {
      if (!this.selectDate) {
        this.$message.error('请选择日期');
        return;
      }

      this.$alert('确认发送邮件', '提示', {
        confirmButtonText: '确定',
        callback: async () => {
          await reSendmailSpecDate({ specDate: this.selectDate })
            .then(() => {
              this.$message.success('发送成功');
            })
            .catch((err) => {
              this.$message.error(err);
            });
        },
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
